﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MarioArena.World;

namespace MarioArena.Characters
{
    public class CooldownBar
    {
        public const int Width = 50;
        public const int Height = 8;
        private const int borderSize = 2;
        private int marginTop = 27;
        private Color color = Color.Yellow;
        private Character mCharacter;

        private Rectangle mBorderBounds;
        private Rectangle mContentBounds;
        private Rectangle mProgressBounds;

        private Texture2D mDummyTexture;

        private bool mUsedByPlayer;
        private float progress = 0.0f; //progress (0 = empty, 1 = full)

        public CooldownBar(Character character, bool usedByPlayer)
        {
            this.mCharacter = character;
            this.mUsedByPlayer = usedByPlayer;

            // Dummy texture
            this.mDummyTexture = new Texture2D(GameManager.GraphicsDevice, 1, 1);
            this.mDummyTexture.SetData(new Color[] { Color.White });
        }

        /// <summary>
        /// Updates the cooldown bar.
        /// </summary>
        /// <param name="lastUse">Last time the cooldown was reset.</param>
        /// <param name="cooldownInMs">Cooldown time in milliseconds.</param>
        public void Update(long lastUse, int cooldownInMs)
        {
            //CALCULATE PROGRESS
            long currentTime = DateTime.Now.Ticks / 10000;
            int difference = (int)(currentTime - lastUse);

            //if cooldown has not yet been completed, calculate progress.
            if (difference < cooldownInMs)
                progress = (float)((float)difference / (float)cooldownInMs);
            else progress = 1.0f;

            //First update the progress will be smaller than zero. Compensate this.
            if (progress < 0)
                progress = 1.0f;

            //CALCULATE ALL BOUNDS
            int screenPosX = (int)this.mCharacter.Position.X + (this.mCharacter.Width / 2) - (Width / 2);
            int screenPosY = (int)this.mCharacter.Position.Y - this.mCharacter.Height - Height + marginTop;
            this.mBorderBounds = new Rectangle(screenPosX, screenPosY + borderSize, Width, Height - borderSize);
            this.mContentBounds = new Rectangle(screenPosX + borderSize, screenPosY + borderSize, Width - (2 * borderSize), Height - (2 * borderSize));
            this.mProgressBounds = new Rectangle(screenPosX + borderSize, screenPosY + borderSize, (int)((Width - (2* borderSize)) * this.progress), Height - (2 * borderSize));
        }

        /// <summary>
        /// Draws the health bar.
        /// </summary>
        public void Draw(SpriteBatch spriteBatch)
        {
            //border
            spriteBatch.Draw(this.mDummyTexture, this.mBorderBounds, Color.DimGray);
            //background of content
            spriteBatch.Draw(this.mDummyTexture, this.mContentBounds, Color.Gray);
            //content
            spriteBatch.Draw(this.mDummyTexture, this.mProgressBounds, Color.Yellow);
        }
    }
}
